package com.zggk.lzz.land;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ToastUtils;
import com.zggk.lzz.R;
import com.zggk.lzz.api.MyApplication;
import com.zggk.lzz.mvp.MVPBaseActivity;
import com.zggk.lzz.roadh.RoadHActivity;
import com.zggk.lzz.roadz.RoadZActivity;
import com.zggk.lzz.util.Utils;
import com.lxj.xpermission.PermissionConstants;
import com.lxj.xpermission.XPermission;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class LandActivity extends MVPBaseActivity<LandContract.View, LandPresenter> implements LandContract.View {
    @Bind(R.id.land_username)
    EditText landUsername;
    @Bind(R.id.land_password)
    EditText landPassword;
    @Bind(R.id.land_bu)
    TextView landBu;
    @Bind(R.id.land_lin)
    LinearLayout landLin;
    private String username = "";
    private String password = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        initview();
        listener();
    }

    @Override
    protected int getLayout() {
        return R.layout.act_land;
    }

    private void listener() {
        landBu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                XPermission xPermission;
                xPermission = XPermission.create(LandActivity.this, PermissionConstants.STORAGE//存储控件
                        , PermissionConstants.LOCATION//位置信息
                        , PermissionConstants.CAMERA//相机
                        , PermissionConstants.MICROPHONE//录制音频
                        , PermissionConstants.PHONE//拨打电话
                );
                xPermission.callback(new XPermission.SimpleCallback() {
                    @Override
                    public void onGranted() {
                        hideSoftInputFromWindow(LandActivity.this);
                        landBu.setClickable(false);
                        username = landUsername.getText().toString().trim();
                        password = landPassword.getText().toString().trim();
                        if (Utils.isNull(username) && Utils.isNull(password)) {
                            Toast.makeText(LandActivity.this, "请输入正确的账号或密码", Toast.LENGTH_LONG).show();
                            return;
                        } else {
                            LandJson landJsons = new LandJson();
                            landJsons.setUserCode(username);
                            landJsons.setPassword(password);
                            String sss = JSON.toJSONString(landJsons);
                            mPresenter.Land(sss, LandActivity.this);
                        }
                    }

                    @Override
                    public void onDenied() {
                        Toast.makeText(LandActivity.this, "没有权限，无法使用该功能", Toast.LENGTH_LONG).show();
                    }
                }).request();
            }
        });
    }
    /**
     * 隐藏输入面板
     *
     * @param activity
     * @return true 成功隐藏面板，false 没有隐藏面板或者没有面板可以隐藏
     */
    public static boolean hideSoftInputFromWindow(Activity activity) {
        if (activity != null) {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                return imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
            }
        }
        return false;
    }
    private void initview() {
        if (!Utils.isNull(MyApplication.spUtils.getString("UserName"))) {
            landUsername.setText(MyApplication.spUtils.getString("UserName"));
        }
        if (!Utils.isNull(MyApplication.spUtils.getString("PassWord"))) {
            landPassword.setText(MyApplication.spUtils.getString("PassWord"));
        }
    }

    @Override
    public void onRequestError(String msg) {
        if (msg.equals("加载中...")){
            mPresenter.getUserInfo();
        }
        landBu.setClickable(true);
        Toast.makeText(LandActivity.this, msg, Toast.LENGTH_LONG).show();
    }

    @Override
    public void getLand(LandBean.DataBean s) {
        mPresenter.getUserInfo();
    }

    @Override
    public void getUserInfos(String json) {
        try {
            JSONObject jsonObject = new JSONObject(json);
            String str1 = jsonObject.optString("code");
            if (str1.equals("200")) {
                UserInfo landBean = JSON.parseObject(json, UserInfo.class);
                MyApplication.spUtils.put("UserName", username);
                MyApplication.spUtils.put("PassWord", password);
                MyApplication.spUtils.put("UserID", landBean.getData().getRecords().getId());
                MyApplication.spUtils.put("OrgName", landBean.getData().getRecords().getOrgName());
                MyApplication.spUtils.put("OrgCode", landBean.getData().getRecords().getOrgCode());
                MyApplication.spUtils.put("OrgId", landBean.getData().getRecords().getOrgId());
                MyApplication.spUtils.put("ZipCode", landBean.getData().getRecords().getZipCode());
                MyApplication.spUtils.put("Name", landBean.getData().getRecords().getRoleName());
                MyApplication.spUtils.put("UserName", landBean.getData().getRecords().getUserName());
                MyApplication.spUtils.put("DemonstrationUnit", landBean.getData().getRecords().getDemonstrationUnitType());
                if (landBean.getData().getRecords().getRoleId().contains("726bb579-86f7-11eb-98a5-0242ac110005")) {//县路长
                    startActivity(new Intent(LandActivity.this, RoadZActivity.class));
                    finish();
                } else if (landBean.getData().getRecords().getRoleId().contains("72702f76-86f7-11eb-98a5-0242ac110005")) {//护路员
                    startActivity(new Intent(LandActivity.this, RoadHActivity.class));
                    finish();
                }else {
                    ToastUtils.showShort("当前用户没有路长制权限,请重新登陆");
                    MyApplication.spUtils.clear();
                    startActivity(new Intent(LandActivity.this, LandActivity.class));
                    finish();
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

